package com.zhongxi.guoyuan.view.seller;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.seller.PostitemOrderListInfoAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.seller.OrderInfos;
import com.zhongxi.guoyuan.entity.seller.SingleOrderInfo;
import com.zhongxi.guoyuan.entity.seller.VerifyOrderInfo;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhongxi.guoyuan.R.id.rl_sure;

/**
 * Created by Administrator on 2017/11/8.
 */

public class SellerVerifyActivity extends BaseActivity {

    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.tv_orderId)
    TextView tvOrderId;
    @Bind(R.id.tv_orderNum)
    TextView tvOrderNum;
    @Bind(R.id.tv_orderTime)
    TextView tvOrderTime;
    @Bind(R.id.tv_verifyId)
    TextView tvVerifyId;
    @Bind(R.id.tv_orderState)
    TextView tvOrderState;
    @Bind(R.id.tvUserName)
    TextView tvUserName;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_sure)
    TextView tvSure;
    @Bind(R.id.tv_total_money)
    TextView tvTotalMoney;
    @Bind(R.id.rl_sure)
    RelativeLayout rl_sure;
    @Bind(R.id.listView)
    ListView listView;
    private String trim = "";
    private Map<String, String> map = new HashMap<>();
    private OrderInfos orderInfo;
    private VerifyOrderInfo verifyOrderInfo;
    private String type;
    private String text;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_verify);
        ButterKnife.bind(this);
        trim = getIntent().getStringExtra("trim");     //提货码
        text = getIntent().getStringExtra("text");
        tvTitle.setText("验证清单");
    }

    @Override
    protected void initData() {
        super.initData();
        map.clear();
        type = "verify";
        if (!TextUtils.isEmpty(trim)) {
            map.put("deliverycode", trim);
            getServer(Url.SHOPVERIFYID, map, "", true, null, "post");
        } else if (!TextUtils.isEmpty(text)) {
            map.put("qrcode", text);
            getServer(Url.SELLERQRCODE, map, "", true, null, "post");
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        try {
            Gson gson = new Gson();
            if ("verify".equals(type)) {
                verifyOrderInfo = gson.fromJson(response, VerifyOrderInfo.class);
                if (null != verifyOrderInfo.getSafeobj() && !"".equals(verifyOrderInfo.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(verifyOrderInfo.getSafeobj());
                }
                if (null != verifyOrderInfo.getDeliverycode() && !"".equals(verifyOrderInfo.getDeliverycode())) {
                    tvVerifyId.setText("提货编码:  " + verifyOrderInfo.getDeliverycode());
                } else {
                    tvVerifyId.setText("");
                }
                JSONObject jsonObject = new JSONObject(response);
                if (null != verifyOrderInfo.getOrderInfo() && !"".equals(verifyOrderInfo.getOrderInfo())) {
                    orderInfo = verifyOrderInfo.getOrderInfo();
                    JSONObject user = jsonObject.optJSONObject("user");
                    JSONObject object = user.optJSONObject(orderInfo.getUserid());
                    getOrderInfo(orderInfo, object);
                    JSONObject mirrorInfo = jsonObject.optJSONObject("mirrorInfo");
                    List<SingleOrderInfo> orderInfoList = new ArrayList<>();
                    if (mirrorInfo != null && !"".equals(mirrorInfo)) {
                        JSONArray jsonArray = mirrorInfo.optJSONArray(orderInfo.getId());
                        for (int j = 0; j < jsonArray.length(); j++) {
                            JSONObject objectItem = (JSONObject) jsonArray.get(j);
                            SingleOrderInfo singleOrderInfo = SingleOrderInfo.paseMatch(objectItem);
                            orderInfoList.add(singleOrderInfo);
                        }
                    }
                    listView.setAdapter(new PostitemOrderListInfoAdapter(context, orderInfoList));
                }
            } else if ("picking".equals(type)) {
                BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
                if (null != baseResponse.getSafeobj() && !"".equals(baseResponse.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
                }
                showShortToast("确认提货成功");
                finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getOrderInfo(OrderInfos orderInfo, JSONObject object) {
        tvOrderId.setText("订单编号:  " + orderInfo.getId());
        tvOrderNum.setText("共  " + orderInfo.getGoodsnum() + " 个");
        tvOrderTime.setText("下单时间:  " + orderInfo.getCtime());
        switch (orderInfo.getOrderstatus()) {
            case 1:
                tvOrderState.setText("已备货");
                rl_sure.setVisibility(View.VISIBLE);
                break;
            case 2:
                rl_sure.setVisibility(View.GONE);
                tvOrderState.setText("商家已出货");
                break;
            case 3:
                rl_sure.setVisibility(View.GONE);
                tvOrderState.setText("用户确认提货");
                break;
            case 4:
                rl_sure.setVisibility(View.GONE);
                tvOrderState.setText("已评价");
                break;
        }

        tvUserName.setText(object.optString("username"));
        tvPhone.setText(object.optString("mphone"));
        tvTotalMoney.setText("商品合计:  " + orderInfo.getShouldprice() + " 元");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
        if (null != baseResponse.getSafeobj() && !"".equals(baseResponse.getSafeobj())) {
            MyApplication.getInstance().setSpValue(baseResponse.getSafeobj());
        }
        rl_sure.setVisibility(View.GONE);

        if (baseResponse.getCode() == 1000) {
            showShortToast("该商品不存在,请核对提货码正确后输入");
        } else if (baseResponse.getCode() == 2015) {
            showShortToast("订单镜像丢失");
        } else if (baseResponse.getCode() == 9002) {
            showShortToast("未登录");
        }
        finish();
    }


    @OnClick({R.id.ivBack, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tv_sure:
                map.clear();
                map.put("orderid", orderInfo.getId());
                map.put("deliverycode", verifyOrderInfo.getDeliverycode());
                type = "picking";
                getServer(Url.SELLERSUREPICKING, map, "", true, null, "post");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
